<?php

/**
 * Erra_Fields_Search_PageNumber - Page number combobox for search fields
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Erra_Fields_Search_PageNumber extends Erra_Fields_Base_Field {

    protected $_operator;
    protected $_pageCount = 0;
    public static $realItemCount = array();

    function __construct($objectId) {
        parent::__construct($objectId);
        $this->setDatabaseType('none');
        $this->setPageCount(1);
        $this->setCurrentPage(1);
    }

    /**
     * Shows page number combobox
     *
     * @param mixed $lang
     * @return
     */
    public function editInstance(Array $extraProperties=null) {
        $res = '<div class="erra-form-field-instance">';
        $res .= $this->outputFieldHeader();

        $res .= __('Affichage de la page n°') . ' <select ' . $this->getHtmlAttributes($extraProperties) . '  name="' . $this->getHtmlName() . '[criteria]" id="' . $this->getHtmlId() . '_criteria">';
        foreach ($this->fieldData as $optionKey => $optionValue) {
            $selected_flag = (!is_null($this->getRawData()) && $this->getRawData() == $optionKey) ? ' selected ' : '';
            $res .= '<option ' . $selected_flag . ' value="' . $optionKey . '">' . $optionValue . '</option>';
        }
        $res .= '</select>';
        $res .= '<input type="hidden" name="' . $this->getHtmlName() . '[operator]" id="' . $this->getHtmlId() . '_operator" value="__void"/>';
        $res .= '</div>';
        return $res;
    }

    function toHtml($displayParameters = null) {
        if (isset($this->fieldData) && is_array($this->fieldData) && isset($this->fieldData[$this->getRawData()])) {
            return $this->fieldData[$this->getRawData()];
        }
        return $this->getRawData();
    }

    /** Getters and setters */
    public function setOperator($operator) {
        $this->_operator = $operator;
        return $this;
    }

    public function getOperator() {
        return $this->_operator;
    }

    function setPageCount($pageCount) {
        $pageList = Array();
        for ($i = 1; $i <= $pageCount; $i++) {
            $pageList[$i] = $i;
        }
        $this->setFieldData($pageList);
        $this->_pageCount = $pageCount;
        return $this;
    }

    public function getPageCount() {
        return $this->_pageCount;
    }

    function setCurrentPage($pageNumber) {
        $this->_rawData = $pageNumber;
    }

    public function getCurrentPage() {
        return $this->_rawData;
    }

}